﻿using MoetutuServer.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WordPressSharp;
using WordPressSharp.Models;

namespace MoetutuServer.PostHelper
{
    class WordpressModify
    {/// <summary>
    /// 修改文章
    /// </summary>
    /// <param name="PublistPost"></param>
    /// <param name="UserInfoPath"></param>
        public static void ReEdit(Post PublistPost, string UserInfoPath) {
            using (WordPressClient client = WordPressPostHelper.GetClient(UserInfoPath)) {
                ReEdit(PublistPost, UserInfoPath);
            }
        }
        /// <summary>
        ///  修改文章
        /// </summary>
        /// <param name="PublistPost"></param>
        /// <param name="UserClient"></param>
        public static void ReEdit( Post PublistPost, WordPressClient UserClient) {

            if (PublistPost == null|| UserClient==null)
                return;
            PublishFlag(UserClient.EditPost(PublistPost));
        }
        /// <summary>
        /// 修改文章，需要文章信息
        /// </summary>
        /// <param name="PostId"></param>
        /// <param name="Content"></param>
        /// <param name="UserDatPath"></param>
        public static void Edit(String PostId, string Content, String UserDatPath)
        {

            using (WordPressClient client = WordPressPostHelper.GetClient(UserDatPath))
            {
                if (client == null)
                    return;
                Post post = WordPressPostHelper.GetPost(PostId, client);
                if (post == null)
                    return;
                Console.WriteLine(post.PublishDateTime);
                Console.WriteLine(post.PostType);
                post.PublishDateTime = post.PublishDateTime;
                post.Content = Content;

                Console.WriteLine(post.PublishDateTime);
                //   post.PublishDateTime = DateTime.Now;

                PublishFlag(client.EditPost(post));

            }

        }

  

        private static void PublishFlag(bool Flag)
        {

            if (Flag)
                Console.WriteLine("修改成功");
            else
                Console.WriteLine("修改失败");
        }



        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userDataPath"></param>
        internal static void Delete(string id, string UserDatPath)
        {
            using (WordPressClient client = WordPressPostHelper.GetClient(UserDatPath))
            {


                //记录删除日志
                FileHelper.AppendUTF8Text(StaticValue.TempPath + "NullContent.txt", string.Format("{0}---{1}", DateTime.Now.ToString(), id));
                client.DeletePost(Helper.TextHelper.StringToInt(id));
            }

        }
        /// <summary>
        /// 添加标签
        /// </summary>
        /// <param name="PostId"></param>
        /// <param name="content"></param>
        /// <param name="tempTaglist"></param>
        /// <param name="UserDatPath"></param>
        internal static void AddTag(string PostId, string content, List<string> tempTaglist, string UserDatPath)
        {
            Console.WriteLine("启动标签处理流程");

            using (WordPressClient client = WordPressPostHelper.GetClient(UserDatPath))
            {
                if (client == null)
                    return;
                Post post = WordPressPostHelper.GetPost(PostId, client);
                if (post == null)
                    return;
                List<Term> GetTermList = post.Terms.ToList();
                List<Term> TermsTemp = new List<Term>();
                foreach (string line in tempTaglist)
                {
                    bool JumpFlag = false;
                    foreach (Term SingleTerm in GetTermList)
                    {
                        if (String.Equals(SingleTerm.Name, line.Trim(), StringComparison.CurrentCultureIgnoreCase))
                        {
                            TermsTemp.Add(SingleTerm);
                            Console.WriteLine("比对发现已有，不重复添加");
                            JumpFlag = true;
                            break;
                        }
                     
                    }
                    if (JumpFlag)
                        continue;

                    Term termTemp = WordPressPostHelper.CreateTagHelper(client, line.Trim());
                    if (!GetTermList.Contains(termTemp))
                    {
                        Console.WriteLine("加入标签" + line);
                        TermsTemp.Add(termTemp);
                    }


                }
                post.Terms = TermsTemp.ToArray();
                content= HtmlHelper.RemoveNode(content, "//ul[@class='wp-tag-out']");
                content= HtmlHelper.RemoveNode(content, "//div[@class='all_TagBox']");
                post.Content= HtmlHelper.RemoveNode(content, "//a[@class='all_tag_show']");
                PublishFlag(client.EditPost(post));
            }
        }
    }
}
